package com.zsc.shixun.service.function.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zsc.shixun.contant.Contant;
import com.zsc.shixun.controller.WebSocketServer;
import com.zsc.shixun.entity.Following;
import com.zsc.shixun.entity.User;
import com.zsc.shixun.model.following.FollowingListVO;
import com.zsc.shixun.model.following.FollowingVO;
import com.zsc.shixun.service.IFollowingService;
import com.zsc.shixun.service.IMessageService;
import com.zsc.shixun.service.IUserService;
import com.zsc.shixun.service.function.MyFollowingService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 关注
 * </p>
 *
 * @author ZWYZY
 * @since 2020/6/18
 */
@Service
@Transactional
public class MyFollowingServiceImpl implements MyFollowingService {

    private static final Logger logger = LoggerFactory.getLogger(MyFollowingServiceImpl.class);
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private IUserService userService;

    @Autowired
    private IFollowingService followingService;

    @Autowired
    private IMessageService messageService;

    @Override
    public FollowingListVO list(Long id) {
        if (redisTemplate.hasKey(Contant.USER_FOLLOWING_KEY + id)) {
            return (FollowingListVO) redisTemplate.opsForValue().get(Contant.USER_FOLLOWING_KEY + id);
        }
        FollowingListVO followingListVO = new FollowingListVO();
        followingListVO.setFollowingNumber(userService.getById(id).getFollowingNumber());
        List<Following> followings = followingService.list(new QueryWrapper<Following>().lambda().eq(Following::getUserId, id));
        List<FollowingVO> followingVOList = new ArrayList<>(10);
        followings.forEach(f -> {
            FollowingVO followingVO = new FollowingVO();
            User user = userService.getById(f.getFollowId());
            followingVO.setFollow_id(f.getFollowId());
            followingVO.setImg(user.getImg());
            followingVO.setUsername(user.getUsername());
//            followingVO.setId(f.getId());
            followingVOList.add(followingVO);
        });
//        for (Following f : followings
//        ) {
//            FollowingVO followingVO = new FollowingVO();
//            User user = userService.getById(f.getFollowId());
//            followingVO.setFollow_id(f.getFollowId());
//            followingVO.setImg(user.getImg());
//            followingVO.setUsername(user.getUsername());
////            followingVO.setId(f.getId());
//            followingVOList.add(followingVO);
//        }
        followingListVO.setFollowingVOList(followingVOList);
        redisTemplate.opsForValue().set(Contant.USER_FOLLOWING_KEY + id, followingListVO);
        return followingListVO;
    }

    @Override
    public void delete(Long id, Long uid) {
        boolean flag = followingService.remove(new QueryWrapper<Following>().lambda().eq(Following::getUserId, uid).eq(Following::getFollowId, id));
        User usering = new User();
        usering.setId(uid);
        usering.setFollowingNumber(userService.getById(uid).getFollowingNumber() - 1);
        userService.updateById(usering);

        User usered = new User();
        usered.setId(id);
        usered.setFollowedNumber(userService.getById(id).getFollowedNumber() - 1);
        userService.updateById(usered);

        if (redisTemplate.hasKey(Contant.USER_FOLLOWING_KEY + uid)) {
            redisTemplate.delete(Contant.USER_FOLLOWING_KEY + uid);
        }
        if (redisTemplate.hasKey(Contant.USER_FOLLOWED_KEY + id)) {
            redisTemplate.delete(Contant.USER_FOLLOWED_KEY + uid);
        }
        if (redisTemplate.hasKey(Contant.USER_KEY + id)) {
            redisTemplate.delete(Contant.USER_KEY + id);
        }
        if (redisTemplate.hasKey(Contant.USER_KEY + uid)) {
            redisTemplate.delete(Contant.USER_KEY + uid);
        }

        User sender=userService.getById(uid);
        String message="博主名为"+sender.getUsername()+"的用户取消关注了你";
        try {
            WebSocketServer.sendObjToUser(messageService.insertMessage(id,uid,message),id);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void following(Long id, Long uid) {
        User usering = new User();
        usering.setId(uid);
        usering.setFollowingNumber(userService.getById(uid).getFollowingNumber() + 1);
        userService.updateById(usering);

        User usered = new User();
        usered.setId(id);
        usered.setFollowedNumber(userService.getById(id).getFollowedNumber() + 1);
        userService.updateById(usered);


        Following following = new Following();
        following.setUserId(uid);
        following.setFollowId(id);
        followingService.save(following);
        if (redisTemplate.hasKey(Contant.USER_FOLLOWING_KEY + uid)) {
            redisTemplate.delete(Contant.USER_FOLLOWING_KEY + uid);
        }
        if (redisTemplate.hasKey(Contant.USER_FOLLOWED_KEY + id)) {
            redisTemplate.delete(Contant.USER_FOLLOWED_KEY + uid);
        }
        if (redisTemplate.hasKey(Contant.USER_KEY + id)) {
            redisTemplate.delete(Contant.USER_KEY + id);
        }
        if (redisTemplate.hasKey(Contant.USER_KEY + uid)) {
            redisTemplate.delete(Contant.USER_KEY + uid);
        }
        User sender=userService.getById(uid);

        String message="博主名为"+sender.getUsername()+"的用户关注了你";
        try {
            WebSocketServer.sendObjToUser(messageService.insertMessage(id,uid,message),id);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean isFollow(Long id, Long uid) {
        Following following = followingService.getOne(new QueryWrapper<Following>().lambda().eq(Following::getUserId, uid).eq(Following::getFollowId, id));
        if (following != null) {
            return true;
        } else {
            return false;
        }
    }
}
